/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package zombiedefense.framework;

import zombiedefense.effects.BloodSpurt;
import zombiedefense.input.InputManager;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.geom.Point2D;
import zombiedefense.effects.GunFlash;
import zombiedefense.effects.Spark;
import zombiedefense.effects.Tracer;
import zombiedefense.map.TileMap;
import zombiedefense.util.MathEx;
import zombiedefense.util.ObjectPool;
import static zombiedefense.util.MathEx.loadImageFromJar;

/**
 *
 * @author &{user}
 */
public class EffectManager extends GameObjectManager{
    
    private Image[] images = {
        loadImageFromJar("/images/pistolflash1.png"),
	loadImageFromJar("/images/shotgunflash1.png"),
        loadImageFromJar("/images/assaultrifleflash1.png"),
        loadImageFromJar("/images/mgflash1.png"),
	loadImageFromJar("/images/bulletimpact.png")
    };
    
    public static final byte pistolFlash = 0;
    public static final byte shotgunFlash = 1;
    public static final byte assaultRifleFlash = 2;
    public static final byte machineGunFlash = 3;
    public static final byte bulletImpact = 4;
    
    public static final long tracerTime = 2;
    public static final long sparkTime = 10;
    public static final long flashTime = 5;
    public static final long spurtTime = 100;
    
    private static final int maxFlashes = 3;
    private static final int maxSparks = 25;
    private static final int maxTracers = 25;
    private static final int maxSpurts = 10;
    
    private static final double tracerPct = 50.0;
		
    //pools of effect objects to conserve memory allocation time
    private ObjectPool<GunFlash> gunFlashPool;
    private ObjectPool<Spark> sparkPool;
    private ObjectPool<Tracer> tracerPool;
    private ObjectPool<BloodSpurt> spurtPool;
    
    public EffectManager(Dimension gameDims, TileMap map,InputManager input) {
	super(gameDims, map,input);
	
	gunFlashPool = new ObjectPool(GunFlash.class,maxFlashes);
	sparkPool = new ObjectPool(Spark.class,maxSparks);
	tracerPool = new ObjectPool(Tracer.class,maxTracers);
	spurtPool = new ObjectPool(BloodSpurt.class,maxSpurts);
    }
    
    /*
     * Effect adding methods retreive a previously used object from a pool,
     * recycle it, and use it again. If there are no objects currently free,
     * the call is ignored.
     */
    
    public void addGunFlash(int flashtype,Point2D.Double barrel,double rotation){
        Image i = images[flashtype];
	GunFlash g = gunFlashPool.getPooledObject();
	if(g != null){
	    g.setFields(0,0,flashTime,i,rotation,barrel);   //x and y are ignored
	    add(g);
	}
    }
    
    public void addSpark(Point2D.Double impact,double direction){
	Spark s = sparkPool.getPooledObject();
	if(s != null){
	    s.setFields(impact.x,impact.y,sparkTime,direction + Math.PI);
	    add(s);
	}
    }
    
    public void addTracer(Point2D.Double impact,Point2D.Double barrel){
        
        if(!MathEx.percent(tracerPct)) return;
        
	Tracer t = tracerPool.getPooledObject();
        if(t != null){
                t.setFields(impact.x,impact.y,barrel.x,barrel.y,tracerTime);
                add(t);
        }else{
            System.out.println("All tracers used!");
        }
    }
    
    public void addBloodSpurt(Point2D.Double impact){
	BloodSpurt s = spurtPool.getPooledObject();
	
	if(s != null){
	    s.setFields(impact.x,impact.y,spurtTime);
	    add(s);
	}
    }
}
